package com.example.smart2.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smart2.R;
import com.example.smart2.model.AppModel;
import com.example.smart2.ui.PressDetailsActivity;
import com.example.smart2.ui.service.StopCarActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class AppAdapter extends RecyclerView.Adapter<AppAdapter.ViewHolder> {
    private Context context;
    private AppModel appModel;
    private Activity activity;

    public AppAdapter(Context context,AppModel appModel){
        this.context=context;
        this.appModel=appModel;
    }

    public AppAdapter(Context context,AppModel appModel,Activity activity){
        this.context=context;
        this.appModel=appModel;
        this.activity=activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.item_app,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        AppModel.RowsBean rowsBean=appModel.getRows().get(position);
        String name=rowsBean.getServiceName();
        String url=rowsBean.getImgUrl()+"";
        String str="http://124.93.196.45:10001";
        holder.textView.setText(name);
        if(url.equals("local")){
            Glide.with(context)
                    .load(rowsBean.getRemark())
                    .placeholder(R.drawable.slide1)
                    .into(holder.imageView);
        } else{
            Glide.with(context)
                    .load(str+rowsBean.getImgUrl())
                    .placeholder(R.drawable.ic_baseline_cloud_download_24)
                    .into(holder.imageView);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(name.equals("更多服务")){
                    if(activity!=null){
                        BottomNavigationView navView=activity.findViewById(R.id.nav_view);
                        navView.setSelectedItemId(R.id.navigation_dashboard);
                    }
                } else {
                    if(name.equals("停哪儿")){
                        Intent intent=new Intent(context, StopCarActivity.class);
                        intent.putExtra("str",rowsBean.getServiceName());
                        context.startActivity(intent);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return appModel.getRows().size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=(ImageView) itemView.findViewById(R.id.imageView);
            textView=(TextView) itemView.findViewById(R.id.textView);
        }
    }
}
